package com.weitu.accessory.controller;

import com.weitu.accessory.bean.Accessory;
import com.weitu.accessory.bean.AccessoryPackageInfo;
import com.weitu.accessory.service.AccessoryUtilService;
import com.weitu.accessory.util.DownloadAccessoryUtil;
import com.weitu.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by fanyuting on 2017/3/20 0020.
 */
@Controller
@RequestMapping("/accessory")
public class AccessoryController extends BaseController {
    @Autowired
    private AccessoryUtilService accessoryUtilService;

    /***
     * 跳转到附件上传页面
     * @return
     */
    @RequestMapping("/toAccessoryPage")
    public String toAccessoryPage(Model model) {
        String businessId = "123abc";//业务Id
        List<AccessoryPackageInfo> accessoryList = accessoryUtilService.queryAccessoryByBusinessId(businessId);
        model.addAttribute("accessoryList", accessoryList);
        model.addAttribute("uploadUserId", "123456");
        //model.addAttribute("businessId","123456abcd");
        model.addAttribute("packageKey", "abcdef");
        return "WEB-INF/jsp/accessory/accessory";
    }

    /**
     * 附件上传controller
     *
     * @param files
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("upload")
    public String upload(@RequestParam("files") MultipartFile[] files, HttpServletRequest request) {

        String packageKey = request.getParameter("packageKey");//包Key
        String userId = request.getParameter("uploadUserId");//上传用户Id
        String accessoryIds = accessoryUtilService.uploadAccessory(files, userId, packageKey);
        return accessoryIds;
    }

    /**
     * 附件下载controller
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("download")
    public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String accessoryId = request.getParameter("accessoryId");
        Accessory accessoryOption = new Accessory();
        accessoryOption.setId(accessoryId);
        List<Accessory> accessorys = accessoryUtilService.queryAccessory(accessoryOption);
        Accessory accessory = accessorys.get(0);
        String filePath = accessory.getUrl();
        String fileName = accessory.getFileName();
        DownloadAccessoryUtil.downloadFile(request, response, filePath, fileName);

    }

    /**
     * 附件删除controller
     * @param request
     * @param response
     * @return
     */
   /* @ResponseBody
    @RequestMapping("deleteAccesory")
    public String deleteAccesory(HttpServletRequest request, HttpServletResponse response){

        String packageKey = request.getParameter("packageKey");//包Key
        String userId = request.getParameter("uploadUserId");//上传用户Id
        //request.getParameter("accessoryIds");
        return "";
    }*/

    /**
     * 测试form表单提交
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("testForm")
    public String testForm(HttpServletRequest request) {
        String[] addAccessoryIds = request.getParameterValues("add_accessoryIds");
        String[] deleteAccessoryIds = request.getParameterValues("delete_accessoryIds");
        String businessId = "123abc";//业务id
        boolean flag = true;
        //更新新增附件的业务id
        if (addAccessoryIds != null && addAccessoryIds.length != 0) {
            flag = accessoryUtilService.updateAccessoryBusinessId(addAccessoryIds, businessId);
        }
        //删除附件
        if (deleteAccessoryIds != null && deleteAccessoryIds.length != 0) {
            flag = accessoryUtilService.deleteAccessory(deleteAccessoryIds);
        }
        if (!flag) {
            return "false";
        }
        return "success";

    }

    @RequestMapping(value = "fileUpload", method = RequestMethod.GET)
    public String upload(Model model, String businessId, String uploadUserId, String packageKey) {
        List<AccessoryPackageInfo> accessoryList = accessoryUtilService.queryAccessoryByBusinessId(businessId);
        model.addAttribute("accessoryList", accessoryList);
        model.addAttribute("uploadUserId", uploadUserId);
        model.addAttribute("packageKey", packageKey);
        return BASE_JSP_PATH + "accessory/fileUpload";
    }
}
